package com.yueshop.cart.vo;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 封装 购物车
 */

public class Cart {

    //购物项
    private List<CartItem> items;
    //购物项数量
    private Integer countNum;
    //商品类型数量
    private  Integer countType;
    //商品总价
    private BigDecimal totalAmount;
    //减免价格 优惠价格
    private BigDecimal reduce =new BigDecimal("0");


    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        //计算所有购物项的总数量
        int count =0;
        if(items!=null&&items.size()>0){
            for (CartItem item : items) {
                count+=item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        if(items!=null&&items.size()>0){
            return  items.size();
        }
        return countType;
    }


    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        //1.计算购物项总价格
        if(items!=null&&items.size()>0){
            for (CartItem item : items) {
            //计算 所有被选中的购物项
                if(item.getCheck()){
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount=amount.add(totalPrice);
                }
            }
        }
        //2.减去优惠价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
